/*
 * @features: 功能
 * @description: 说明
 * @Date: 2020-11-02 21:51:31
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2020-11-02 23:18:22
 * @LastEditors: judu233
 */

const {ccclass, property} = cc._decorator;


export class LabelUtil {
    static lable: cc.Label;

    /**
     * @param text 要显示文本内容
     * @param designSize label的设计宽高
     * @param fontSize 字体大小
     * @param lineHeight 行高
     */
    public static measureSize(text: string, designSize: cc.Size, fontSize: number, lineHeight: number): cc.Size {
        if(!LabelUtil.lable) {
            let node = new cc.Node();
            LabelUtil.lable = node.addComponent<cc.Label>(cc.Label);
        }
        LabelUtil.lable.fontSize = fontSize;
        LabelUtil.lable.lineHeight = lineHeight;
        LabelUtil.lable.string = text;

        // 计算宽 
        LabelUtil.lable.overflow = cc.Label.Overflow.NONE;
        LabelUtil.lable.node.setContentSize(new cc.Size(0, lineHeight));
        LabelUtil.lable["_forceUpdateRenderData"](true);
    
        let textWidth = Math.min(LabelUtil.lable.node.width, designSize.width);
        textWidth = Math.floor(textWidth / fontSize) * fontSize;
        // 计算高
        LabelUtil.lable.overflow = cc.Label.Overflow.RESIZE_HEIGHT;
        LabelUtil.lable.node.setContentSize(new cc.Size(textWidth, 0));
        LabelUtil.lable["_forceUpdateRenderData"](true);
        // LabelUtil.lable._updateRenderData(true);
        let textHeight = LabelUtil.lable.node.height;
        return new cc.Size(textWidth, textHeight);
    }
}
